package com.smile.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.filter.SaFirewallCheckFilterForServlet;
import cn.hutool.core.bean.BeanUtil;
import com.smile.commons.page.R;
import com.smile.model.dto.request.AddOrganizationRequestDTO;
import com.smile.model.dto.request.EditOrganizationRequestDTO;
import com.smile.model.dto.request.GetOrganizationListRequestDTO;
import com.smile.model.dto.request.GetOrganizationsByUserIdRequestDTO;
import com.smile.model.request.AddOrganizationRequest;
import com.smile.model.request.EditOrganizationRequest;
import com.smile.model.request.GetOrganizationByIdRequest;
import com.smile.model.request.GetOrganizationListRequest;
import com.smile.model.response.GetOrganizationByIdResponse;
import com.smile.model.response.GetOrganizationByIdsResponse;
import com.smile.model.response.GetOrganizationListResponse;
import com.smile.model.response.response.GetOrganizationByIdResponseDTO;
import com.smile.model.response.response.GetOrganizationByIdsResponseDTO;
import com.smile.model.response.response.GetOrganizationByUserIdResponseDTO;
import com.smile.model.response.response.GetOrganizationListResponseDTO;
import com.smile.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/organization")
@Validated
@CrossOrigin
public class OrganizationController {

    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private SaFirewallCheckFilterForServlet saFirewallCheckFilterForServlet;

    /**
     * 根据id获取组织信息
     *
     * @return
     */
    @SaIgnore
    @PostMapping(value = "/getOrganizationById", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<GetOrganizationByIdResponse> getOrganizationById(@RequestBody GetOrganizationByIdRequest getOrganizationById) {
        GetOrganizationByIdResponseDTO organizationById = organizationService.getOrganizationById(getOrganizationById.getId());
        GetOrganizationByIdResponse getOrganizationByIdResponse = new GetOrganizationByIdResponse();
        BeanUtil.copyProperties(organizationById, getOrganizationByIdResponse);
        return R.ok(getOrganizationByIdResponse);
    }

    /**
     * 获取组织树
     */
    @SaIgnore
    @GetMapping("/getOrganizationsByUserId")
    public R<List<GetOrganizationByUserIdResponseDTO>> organizationTree(@RequestParam Long userId) {

        GetOrganizationsByUserIdRequestDTO getOrganizationsByUserIdRequestDTO = new GetOrganizationsByUserIdRequestDTO();
//        if (ObjectUtil.isNotNull(getOrganizationsByUserIdRequest)) {
//
//            BeanUtil.copyProperties(getOrganizationsByUserIdRequest, getOrganizationsByUserIdRequestDTO);
//        }
        getOrganizationsByUserIdRequestDTO.setUserId(userId);
        List<GetOrganizationByUserIdResponseDTO> list = organizationService.organizationTree(getOrganizationsByUserIdRequestDTO);

        return R.ok(list);
    }


    /**
     * 添加组织
     *
     * @param addOrganizationRequest
     * @return
     */
//    @SaCheckPermission("组织管理")
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Void> addOrganization(@RequestBody @Valid AddOrganizationRequest addOrganizationRequest) {

        AddOrganizationRequestDTO addOrganizationRequestDTO = new AddOrganizationRequestDTO();
        BeanUtil.copyProperties(addOrganizationRequest, addOrganizationRequestDTO);
        Boolean b = organizationService.addOrganization(addOrganizationRequestDTO);
        if (b) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    /**
     * 获取组织列表
     *
     * @param getOrganizationListRequest
     * @return
     */
    @RequestMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE, method = {RequestMethod.POST})
    public R<List<GetOrganizationListResponse>> getOrganizationList(@RequestBody GetOrganizationListRequest getOrganizationListRequest) {
        GetOrganizationListRequestDTO getOrganizationListRequestDTO = new GetOrganizationListRequestDTO();
        BeanUtil.copyProperties(getOrganizationListRequest, getOrganizationListRequestDTO);
        List<GetOrganizationListResponseDTO> getOrganizationListResponseDTOPage = organizationService.getOrganizationList1(getOrganizationListRequestDTO);
        return R.ok(BeanUtil.copyToList(getOrganizationListResponseDTOPage.stream()
                .sorted((i1, i2) -> {
                    return i1.getSort() < i2.getSort() ? i1.getSort() : i2.getSort();
                }).collect(Collectors.toList()), GetOrganizationListResponse.class));
    }

    /**
     * 获取无人机机场的组织列表
     */
    @PostMapping(value = "/getDeviceList")
    public R<List<GetOrganizationListResponse>> getDeviceList(@RequestBody GetOrganizationListRequest getOrganizationListRequest) {
        GetOrganizationListRequestDTO getOrganizationListRequestDTO = new GetOrganizationListRequestDTO();
        BeanUtil.copyProperties(getOrganizationListRequest, getOrganizationListRequestDTO);
        List<GetOrganizationListResponseDTO> getOrganizationListResponseDTOPage = organizationService.getDeviceList(getOrganizationListRequestDTO);
        return R.ok(BeanUtil.copyToList(getOrganizationListResponseDTOPage.stream()
                .sorted((i1, i2) -> {
                    return i1.getSort() < i2.getSort() ? i1.getSort() : i2.getSort();
                }).collect(Collectors.toList()), GetOrganizationListResponse.class));
    }

    @RequestMapping(value = "/getOrganizationByIds", produces = MediaType.APPLICATION_JSON_VALUE, method = {RequestMethod.POST})
    public R<Map<Long, GetOrganizationByIdsResponse>> getOrganizationByIds(@RequestBody List<Long> ids) {


        Map<Long, GetOrganizationByIdsResponseDTO> map = organizationService.getOrganizationByIds(ids);
        Map<Long, GetOrganizationByIdsResponse> hashMap = new HashMap<>();
        map.forEach((i1, i2) -> {
            hashMap.put(i1, BeanUtil.copyProperties(i2, GetOrganizationByIdsResponse.class));
        });

        return R.ok(hashMap);
    }


    /**
     * 编辑组织信息
     *
     * @param editOrganizationRequest
     * @return
     */
//    @SaCheckPermission("组织管理")
    @PostMapping(value = "/edit", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Void> editOrganization(@RequestBody @Valid EditOrganizationRequest editOrganizationRequest) {
        EditOrganizationRequestDTO editOrganizationRequestDTO = new EditOrganizationRequestDTO();
        BeanUtil.copyProperties(editOrganizationRequest, editOrganizationRequestDTO);
        Boolean result = organizationService.editOrganization(editOrganizationRequestDTO);
        if (result) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    //    @SaCheckPermission("组织管理")
    @PostMapping(value = "/delete")
    public R<Void> deleteOrganization(@RequestBody Long id) {
        Boolean result = organizationService.deleteOrganization(id);
        if (result) {
            return R.ok();
        } else {
            return R.fail();
        }
    }


}
